<template>
  <div class="app-container">
    <div class="contentDiv">
      <div class="tx-r" style="margin-bottom:15px">
        <el-button v-permiss="['421020']" size="small" type="primary" round @click="handleAdd">新增</el-button>
        <el-button-group style="margin-left:12px">
          <el-button v-permiss="['421070']" size="small" type="primary" round @click="handImport">导入</el-button>
          <el-button v-permiss="['421070']" size="small" type="primary" :disabled="tableData.length===0" round @click="exportBook">导出</el-button>
        </el-button-group>
      </div>
      <el-table :key="tableKey" v-loading="listLoading" :data="tableData" size="small" stripe fit highlight-current-row style="width: 100%;">
        <el-empty slot="empty" :image-size="120" />
        <el-table-column label="姓名" min-width="100">
          <template slot-scope="{row}">
            <span>{{ row.employeeName }}</span>
          </template>
        </el-table-column>
        <el-table-column label="证照类型" min-width="100">
          <template slot-scope="{row}">
            <span>{{ row.certificateName }}</span>
          </template>
        </el-table-column>
        <el-table-column label="证照号码" min-width="160">
          <template slot-scope="{row}">
            <span>{{ row.certificateNumber }}</span>
          </template>
        </el-table-column>
        <el-table-column label="国籍" align="center" width="100">
          <template slot-scope="{row}">
            <span>{{ row.nationalityName }}</span>
          </template>
        </el-table-column>
        <el-table-column label="状态" align="center" width="100">
          <template slot-scope="{row}">
            <span>{{ row.status==1?'正常':'不正常' }}</span>
          </template>
        </el-table-column>
        <el-table-column label="手机号" align="center" width="140">
          <template slot-scope="{row}">
            <span>{{ row.tel }}</span>
          </template>
        </el-table-column>
        <el-table-column label="部门" align="left" width="100">
          <template slot-scope="{row}">
            <span>{{ row.deptName }}</span>
          </template>
        </el-table-column>
        <el-table-column label="入职日期" align="center" width="120">
          <template slot-scope="{row}">
            <span>{{ row.comeDate }}</span>
          </template>
        </el-table-column>
        <el-table-column label="离职日期" align="center" width="120">
          <template slot-scope="{row}">
            <span>{{ row.endDate }}</span>
          </template>
        </el-table-column>
        <el-table-column label="是否雇员" align="center" width="100">
          <template slot-scope="{row}">
            <span>{{ row.isEmployee==1?'是':'否' }}</span>
          </template>
        </el-table-column>
        <el-table-column label="备注" align="left" width="120" show-overflow-tooltip>
          <template slot-scope="{row}">
            <span>{{ row.remarks }}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" fixed="right" align="center" width="100">
          <template slot-scope="{row}">
            <el-button v-permiss="['421020']" type="text" size="small" @click="handleCompile(row)">编辑</el-button>
            <el-button v-permiss="['421060']" type="text" size="small" @click="handleDel(row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <pagination v-show="total>0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit" @pagination="getList" />
    </div>
    <el-dialog :title="dialogStatus=='create'?'新增员工':'编辑员工'" :visible.sync="dialogVisible1" width="620px">
      <el-form ref="dataForm" :model="form" :rules="rules" inline label-position="left" label-width="72px" style="margin-left:20px;">
        <el-form-item label="员工编号" style="margin-right:20px" prop="code">
          <el-input v-model="form.employeeCode" placeholder="员工编号" />
        </el-form-item>
        <el-form-item label="姓名" prop="name">
          <el-input v-model="form.employeeName" placeholder="姓名" />
        </el-form-item>
        <el-form-item label="国籍" style="margin-right:20px">
          <el-select v-model="form.nationalityType" placeholder="国籍" style="width:185px" class="filter-item">
            <el-option v-for="item in nationList" :key="item.id" :label="item.name" :value="item.code" />
          </el-select>
        </el-form-item>
        <el-form-item label="证件类型">
          <el-select v-model="form.certificateType" placeholder="证件类型" style="width:185px" class="filter-item">
            <el-option v-for="item in certificateList" :key="item.id" :label="item.name" :value="item.code" />
          </el-select>
        </el-form-item>
        <el-form-item label="证件号码" style="margin-right:20px">
          <el-input v-model="form.certificateNumber" placeholder="证件号码" />
        </el-form-item>
        <el-form-item label="联系电话">
          <el-input v-model="form.tel" placeholder="联系电话" />
        </el-form-item>
        <el-form-item label="入职日期" style="margin-right:20px">
          <el-date-picker v-model="form.comeDate" :editable="false" type="date" placeholder="入职日期" style="width:185px" :clearable="false" value-format="yyyy-MM-dd" />
        </el-form-item>
        <el-form-item label="离职日期">
          <el-date-picker v-model="form.endDate" :editable="false" type="date" placeholder="离职日期" style="width:185px" :clearable="false" value-format="yyyy-MM-dd" />
        </el-form-item>
        <el-form-item label="部门" style="margin-right:20px">
          <el-select v-model="form.deptId" placeholder="部门" style="width:185px" class="filter-item">
            <el-option v-for="item in deptList" :key="item.id" :label="item.deptName" :value="item.id" />
          </el-select>
        </el-form-item>
        <el-form-item label="人员状态">
          <el-radio v-model="form.status" :label="1">正常</el-radio>
          <el-radio v-model="form.status" :label="0">非正常</el-radio>
        </el-form-item>
        <el-form-item label prop="isEmployee">
          <el-checkbox v-model="form.isEmployee" :false-label="0" :true-label="1">雇员</el-checkbox>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer" align="center">
        <el-button @click="dialogVisible1 = false">取消</el-button>
        <el-button type="primary" @click="handleSave()">确定</el-button>
      </div>
    </el-dialog>
    <el-dialog title="人员导入" :visible.sync="dialogVisible2" width="500px">
      <el-form ref="importForm" :model="importForm" label-position="left" label-width="0" style="margin-left:10px;">
        <el-form-item label prop="fileName" :rules="fileRule">
          <el-upload dragle action="/" accept=".xls, .xlsx" :auto-upload="false" :show-file-list="false" :file-list="fileList" :on-change="importFile">
            <div class="fileDiv">
              <span v-if="importForm.fileName" style="margin-right:5px">{{ importForm.fileName }}</span>
              <el-button type="text">{{ fileList.length === 0 ? '点击上传' : '重新上传' }}</el-button>
            </div>
          </el-upload>
          <el-button size="mini" type="text" @click="downloadModel">下载模板</el-button>
        </el-form-item>
        <p style="margin-top:28px">
          <el-checkbox v-model="importForm.updateDept" :false-label="0" :true-label="1">如果部门编码不存在,同时在-设置-基本档案中新增部门</el-checkbox>
        </p>
        <p>
          <el-checkbox v-model="importForm.updateStaff" :false-label="0" :true-label="1">如果员工编码不存在,同时在-设置-基本档案中新增员工</el-checkbox>
        </p>
        <p>注：如果按证照类型+证照号码存在人员重复,会按最新数据自动更新人员</p>
      </el-form>
      <div slot="footer" class="dialog-footer" align="center">
        <el-button @click="dialogVisible2 = false">取消</el-button>
        <el-button type="primary" @click="handleImport()">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import {
  getEmployee,
  getNationalityType,
  getCertificateType,
  saveEmployee,
  importEmployee,
  exportEmployee,
  delEmployee
} from '@/api/hr'
import { getDept } from '@/api/basedata'
import { debounce } from '@/utils'
export default {
  name: 'Employee',
  data() {
    return {
      tableKey: 0,
      downloadModel: debounce(this.downloadFile, 1000, true),
      tableData: [],
      total: 0,
      listLoading: true,
      nationList: [],
      certificateList: [],
      deptList: [],
      listQuery: {},
      importForm: {
        fileName: '',
        updateDept: 1,
        updateStaff: 1
      },
      fileRule: [
        { required: true, message: '请选择要导入的文件！', trigger: 'change' }
      ],
      fileList: [],
      form: {
        id: '',
        employeeCode: '',
        employeeName: '',
        nationalityType: '',
        certificateType: '',
        certificateNumber: '',
        comDate: '',
        deptId: '',
        comeDate: '',
        endDate: '',
        isEmployee: 0,
        status: 1,
        tel: ''
      },
      formData: null,
      dialogVisible1: false,
      dialogVisible2: false,
      dialogStatus: '',
      rules: {
        employeeName: [
          { required: true, message: '姓名不能为空', trigger: 'change' }
        ],
        employeeCode: [
          { required: true, message: '编码不能为空', trigger: 'change' }
        ]
      }
    }
  },
  created() {
    this.getList()
    getNationalityType().then((res) => {
      this.nationList = res.data.data
    })
    getCertificateType().then((res) => {
      this.certificateList = res.data.data
    })
    getDept().then((res) => {
      this.deptList = res.data.data
    })
  },
  methods: {
    getList() {
      this.listLoading = true
      getEmployee()
        .then((res) => {
          this.listLoading = false
          this.tableData = res.data.data || []
        })
        .catch(() => {
          this.listLoading = false
        })
    },
    downloadFile() {
      window.location.href = 'business/employee.xlsx'
    },
    handImport() {
      this.fileList = []
      this.importForm.fileName = ''
      this.dialogVisible2 = true
    },
    handFileImport() {
      this.$refs.uploadFile.click()
    },
    importFile(file, fileList) {
      this.formData = new FormData()
      if (fileList.length > 0) {
        this.fileList = [fileList[fileList.length - 1]]
      }
      this.formData.append('file', file.raw)
      this.formData.append('fileName', 'employee.xlsx')
      this.formData.append('updateDept', this.importForm.updateDept)
      this.formData.append('updateStaff', this.importForm.updateStaff)
      this.importForm.fileName = file.name
      this.$refs.importForm.validateField('fileName')
    },
    handleImport() {
      this.$refs.importForm.validate((valid) => {
        if (valid) {
          const obj = this.formData
          importEmployee(obj).then((res) => {
            if (res.data.errorCode === '0') {
              this.$message.success('导入成功')
              this.dialogVisible2 = false
              this.getList()
            }
          })
        } else {
          return false
        }
      })
    },
    handleAdd() {
      this.dialogVisible1 = true
      this.dialogStatus = 'create'
      this.form.id = ''
      for (var key in this.form) {
        this.form[key] = ''
      }
      this.form.isEmployee = 0
      this.form.status = 1
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    handleCompile(obj) {
      this.dialogVisible1 = true
      this.dialogStatus = 'update'
      for (var key in this.form) {
        this.form[key] = obj[key]
      }
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    handleSave() {
      saveEmployee(this.form).then((res) => {
        if (res.data.errorCode === '0') {
          this.dialogVisible1 = false
          this.$message.success(
            this.form.id == '' ? '新增' : '修改' + '人员成功'
          )
        }
      })
    },
    exportBook() {
      exportEmployee(this.listQuery)
    },
    handleDel(id) {
      this.$confirm('确定删除？', '提示', {
        confirmButtonText: '确定',
        closeOnClickModal: false,
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        delEmployee(id).then((res) => {
          if (res.data.errorCode === '0') {
            this.getList()
            this.dialogVisible = false
            this.$message.success('删除成功')
          }
        })
      })
    }
  }
}
</script>
<style lang="scss" scoped>
.fileDiv {
  width: 400px;
  height: 60px;
  border: 1px #d0d0d0 solid;
  line-height: 60px;
  text-align: center;
  border-radius: 6px;
}
.filter-item {
  position: relative;
}
/deep/.el-form-item {
  margin-bottom: 18px;
}
</style>
